﻿using IrisMembership.DataLayer;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using IrisMembership.Web.Authentication;
using StructureMap;

namespace IrisMembership.Web
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            ViewEnginesConfig.RemoveExtraViewEngines(ViewEngines.Engines); // Remove WebForms View Engine
        }

        protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        {

            var context = DependencyResolver.Current.GetService<HttpContextBase>();

            var irisPrincipalService = ObjectFactory.GetInstance<IPrincipalService>();

            context.User = irisPrincipalService.GetCurrent(); // Set the HttpContext's User to our IPrincipal (IrisPrincipalService)
        }

        protected void Application_EndRequest(object sender, EventArgs e)
        {
            ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
        }
    }
}
